package com.ckzp.jfinal.system.controller;

import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.system.cache.SysDataCache;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.system.cache.SysTypeCache;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.model.SysPost;
import com.ckzp.jfinal.system.validate.SysPostValidator;
import com.jfinal.aop.Before;
import com.ckzp.jfinal.system.cache.SysTypeCache;
import com.ckzp.jfinal.plugins.tree.TreeNode;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by wuyang(51594) on 2022-11-04 14:41:11.
 */
public class SysPostController extends BaseController {

    @Admins(roles = "login", name = "系统岗位默认", log = false)
    public void index() {
        renderText("系统岗位默认");
    }


    /**
     * 系统岗位列表显示页面
     */
    @Admins(roles = "system", name = "系统岗位列表List", log = false)
    public void SysPostList() {
        FormData fd = FormData.dao();
        TreeNode tree = TreeNode.newNode("page1").setTitle("测试分类").setSpread(true).setTurn(false);
        tree = SysTypeCache.n().getTypeDetailTree(tree, "test_type");
        fd.addData("tree", toJsonStr(TreeNode.newNodeList(tree)));
        fd.addData("def_id", tree.getDatas().get("def_id"));
        fd.addData("def_title", tree.getDatas().get("def_title"));

        setAttr("form", fd);
        render("SysPostList.html");
    }

    /**
     * 系统岗位列表数据查询API
     * fd.addSelect("", SysDataCache.getFormSelect(""));
     */
    @Admins(roles = "system", name = "系统岗位列表数据查询Api", log = false)
    public void SysPostListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_post");

        ps.orderby(getRequest(), "post_id desc");
        Page<SysPost> pages = ps.paginate(SysPost.dao);
        List<SysPost> datalist = pages.getList();
        for (SysPost post : datalist) {

        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加系统岗位Page
     */
    @Admins(roles = "system", name = "添加系统岗位Page", log = false)
    public void AddSysPost() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("post", new SysPost());
        setAttr("form", fd);
        render("ModifySysPost.html");
    }

    /**
     * 修改系统岗位Page
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改系统岗位Page", log = false)
    public void EditSysPost() {
        String post_id = getPara("post_id", "");
        SysPost o = SysPost.dao.findById(post_id);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("post", new SysPost());
        if (o != null) {

        }
        setAttr("form", fd);
        setAttr("post", o);
        render("ModifySysPost.html");
    }

    /**
     * 修改系统岗位Api
     */
    @Before(SysPostValidator.class)
    @Admins(roles = "system", name = "修改系统岗位Api", log = true)
    public void ModifySysPostApi() {
//取得数据
        Datas data = getDatas();
        String post_id = data.getStr("post_id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysPost.dao.findById(post_id)
                    .set("post_name", data.getStr("post_name"))
                    .set("sindex", data.getInt("sindex"))
                    .set("post_desc", data.getStr("post_desc"))

                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysPost()
                    .set("post_name", data.getStr("post_name"))
                    .set("sindex", data.getInt("sindex"))
                    .set("post_desc", data.getStr("post_desc"))

                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改系统岗位字段Api
     * demo:
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改系统岗位字段Api", log = true)
    public void ModifySysPostFieldApi() {
        String post_id = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysPost o = SysPost.dao.findById(post_id);
        if (o != null) {
            boolean status = o.set(field, value).update();
            renderJson(JsonResult.n(status ? 0 : 9, "修改" + (status ? "成功" : "失败")));
        } else {
            renderJson(JsonResult.n(0, "修改成功"));
        }
    }


    /**
     * 删除系统岗位Api
     */
    @Admins(roles = "system", name = "删除系统岗位Api", log = true)
    public void DelSysPostApi() {
        int post_id = getInt("post_id", 0);
        SysPost post = SysPost.dao.findById(post_id);
        if (post == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (post.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
///////////////////////////////////
    }


    /**
     * 批量删除系统岗位Api
     */
    @Admins(roles = "system", name = "批量删除系统岗位Api", log = true)
    public void DelMoreSysPostApi() {
        renderJson(DelMoreModel(SysPost.dao, getParaStr("keys", ""), ""));
    }

    /**
     * 系统岗位图片上传接口
     */
    @Admins(roles = "system", name = "系统岗位图片上传Api", log = false)
    public void UploadImages() {
        renderJson(UpImage.n().upload(getRequest(), "show", true, true));
    }


    /**
     * 改变系统岗位字段状态Api(0 or 1)
     */
    @Admins(roles = "system", name = "改变系统岗位字段状态Api", log = true)
    public void ChangeSysPostTagApi() {
        String keyid = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysPost.dao, keyid, field, msg));
    }
}